﻿using System.Text;

namespace msilgc.cbuilder.CSyntaxTree
{
    internal class SwitchStatement : ICStatement
    {
        private readonly string[] _targets;
        private readonly IVariable _offsetVar;

        public SwitchStatement(string[] targets, IVariable offsetVar)
        {
            _offsetVar = offsetVar;
            _targets = targets;
        }

        public void Emit(StringBuilder builder)
        {
            int offset = (-1);
            foreach (string target in _targets)
            {
                builder.Append("if(");
                _offsetVar.Emit(builder);
                builder.Append(" == ");
                builder.Append(++offset);
                builder.Append("){ goto ");
                builder.Append(target);
                builder.AppendLine("; }");
            }
        }
    }
}
